﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class MessageDAO:IData<Message> 
 	{
		public MessageDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref Message item, int t) {
			 return connec.ExecuteNonQuery("sp_Message", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Message.c_id, item.id), new SqlParameter("@" + Message.c_userid_send, item.userid_send),
				 new SqlParameter("@" + Message.c_userid_receive, item.userid_receive), new SqlParameter("@" + Message.c_contents, item.contents),
				 new SqlParameter("@" + Message.c_createdate, item.createdate));
		}

		public override Message GetItem(IDataReader dr)
		{
			Message obj = new Message();
			obj.id = Convert.ToInt64(dr[Message.c_id]);
			obj.userid_send = Convert.ToInt64(dr[Message.c_userid_send]);
			obj.userid_receive = Convert.ToInt64(dr[Message.c_userid_receive]);
			obj.contents = Convert.ToString(dr[Message.c_contents]);
			obj.createdate = Convert.ToDateTime(dr[Message.c_createdate]);
			return obj;
		}

		public override List<Message> GetAll()
		{
			return GetAll("sp_GetAllMessage", CommandType.StoredProcedure);
		}

		public override Message GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt64(id));
		}
		public override Message GetItemById(Message item){
			throw new NotImplementedException();
		}

	}

 }
